//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "UsdLiborSwapIsdaFixPm.h"
using namespace Cephei::QL::Indexes::Swap;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Indexes/SwapIndex.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Instruments/VanillaSwap.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
using namespace Cephei::QL::Instruments;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::CUsdLiborSwapIsdaFixPm (Cephei::QL::Times::IPeriod^ tenor, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ h) : CSwapIndex(CUsdLiborSwapIsdaFixPm::typeid)
{
    CPeriod^ _Ctenor;
    CYieldTermStructure^ _Ch;
    try
    {
#ifdef HANDLE
        _phUsdLiborSwapIsdaFixPm = NULL;
#endif
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (h))
        {
            _Ch = safe_cast<CYieldTermStructure^> (h->Value);
            _Ch->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _h = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (h) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_Ch->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppUsdLiborSwapIsdaFixPm = new boost::shared_ptr<QuantLib::UsdLiborSwapIsdaFixPm> (new QuantLib::UsdLiborSwapIsdaFixPm ( _tenor,  _h ));
        SetSwapIndex (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppUsdLiborSwapIsdaFixPm));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ch != nullptr) _Ch->Unlock();
    }
}
Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::CUsdLiborSwapIsdaFixPm (Cephei::QL::Times::IPeriod^ tenor, Cephei::QL::Termstructures::IYieldTermStructure^ forwarding, Cephei::QL::Termstructures::IYieldTermStructure^ discounting) : CSwapIndex(CUsdLiborSwapIsdaFixPm::typeid)
{
    CPeriod^ _Ctenor;
    CYieldTermStructure^ _Cforwarding;
    CYieldTermStructure^ _Cdiscounting;
    try
    {
#ifdef HANDLE
        _phUsdLiborSwapIsdaFixPm = NULL;
#endif
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        _Cforwarding = safe_cast<CYieldTermStructure^> (forwarding);
        _Cforwarding->Lock();
        Handle<QuantLib::YieldTermStructure>& _forwarding = static_cast<Handle<QuantLib::YieldTermStructure>&> (_Cforwarding->GetHandle ()); 
        _Cdiscounting = safe_cast<CYieldTermStructure^> (discounting);
        _Cdiscounting->Lock();
        Handle<QuantLib::YieldTermStructure>& _discounting = static_cast<Handle<QuantLib::YieldTermStructure>&> (_Cdiscounting->GetHandle ()); 
        _ppUsdLiborSwapIsdaFixPm = new boost::shared_ptr<QuantLib::UsdLiborSwapIsdaFixPm> (new QuantLib::UsdLiborSwapIsdaFixPm ( _tenor,  _forwarding,  _discounting ));
        SetSwapIndex (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppUsdLiborSwapIsdaFixPm));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Cforwarding != nullptr) _Cforwarding->Unlock();
        if (_Cdiscounting != nullptr) _Cdiscounting->Unlock();
    }
}
Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::CUsdLiborSwapIsdaFixPm (boost::shared_ptr<QuantLib::UsdLiborSwapIsdaFixPm>& childNative, Object^ owner) : CSwapIndex(CUsdLiborSwapIsdaFixPm::typeid)
{
#ifdef HANDLE
	_phUsdLiborSwapIsdaFixPm = NULL;
#endif
	_ppUsdLiborSwapIsdaFixPm = &childNative;
    _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppUsdLiborSwapIsdaFixPm));
}
Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::CUsdLiborSwapIsdaFixPm (QuantLib::UsdLiborSwapIsdaFixPm& childNative, Object^ owner) : CSwapIndex(CUsdLiborSwapIsdaFixPm::typeid)
{
#ifdef HANDLE
	_phUsdLiborSwapIsdaFixPm = NULL;
#endif
	_ppUsdLiborSwapIsdaFixPm = new boost::shared_ptr<QuantLib::UsdLiborSwapIsdaFixPm> (&childNative);
    _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppUsdLiborSwapIsdaFixPm));
    _UsdLiborSwapIsdaFixPmOwner = owner;
    _SwapIndexOwner = owner;
}

Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::CUsdLiborSwapIsdaFixPm (CUsdLiborSwapIsdaFixPm^ copy) : CSwapIndex(CUsdLiborSwapIsdaFixPm::typeid)
{
#ifdef HANDLE
	_phUsdLiborSwapIsdaFixPm = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppUsdLiborSwapIsdaFixPm = new boost::shared_ptr<QuantLib::UsdLiborSwapIsdaFixPm> (copy->GetShared());
        _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppUsdLiborSwapIsdaFixPm));
    }
}
Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::CUsdLiborSwapIsdaFixPm (PLATFORM::Type^ t) : CSwapIndex(CUsdLiborSwapIsdaFixPm::typeid)
{
#ifdef HANDLE
	_phUsdLiborSwapIsdaFixPm = NULL;
#endif
	if (!t->IsSubclassOf(CUsdLiborSwapIsdaFixPm::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::CUsdLiborSwapIsdaFixPm (QuantLib::Handle<QuantLib::UsdLiborSwapIsdaFixPm>& childNative, Object^ owner)  : CSwapIndex(CUsdLiborSwapIsdaFixPm::typeid)
{
	_phUsdLiborSwapIsdaFixPm = &childNative;
	_ppUsdLiborSwapIsdaFixPm = &static_cast<boost::shared_ptr<QuantLib::UsdLiborSwapIsdaFixPm>>(childNative.currentLink());
    _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppUsdLiborSwapIsdaFixPm));
    _UsdLiborSwapIsdaFixPmOwner = owner;
}
Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::CUsdLiborSwapIsdaFixPm (QuantLib::Handle<QuantLib::UsdLiborSwapIsdaFixPm> childNative)  : CSwapIndex(CUsdLiborSwapIsdaFixPm::typeid)
{
	_phUsdLiborSwapIsdaFixPm = &childNative;
	_ppUsdLiborSwapIsdaFixPm = &static_cast<boost::shared_ptr<QuantLib::UsdLiborSwapIsdaFixPm>>(childNative.currentLink());
    _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppUsdLiborSwapIsdaFixPm));
}
#endif
#ifdef STRUCT
Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::CUsdLiborSwapIsdaFixPm (QuantLib::UsdLiborSwapIsdaFixPm childNative)  : CSwapIndex(CUsdLiborSwapIsdaFixPm::typeid)
{
#ifdef HANDLE
	_phUsdLiborSwapIsdaFixPm = NULL;
#endif
	_ppUsdLiborSwapIsdaFixPm = new boost::shared_ptr<QuantLib::UsdLiborSwapIsdaFixPm> (new QuantLib::UsdLiborSwapIsdaFixPm (childNative));
    _ppSwapIndex = new boost::shared_ptr<QuantLib::SwapIndex> (boost::dynamic_pointer_cast<QuantLib::SwapIndex> (*_ppUsdLiborSwapIsdaFixPm));
}
#endif

Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::~CUsdLiborSwapIsdaFixPm ()
{
    if (_ppUsdLiborSwapIsdaFixPm != NULL)
    {
	    delete _ppUsdLiborSwapIsdaFixPm;
        _ppUsdLiborSwapIsdaFixPm = NULL;
    }
}
Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::!CUsdLiborSwapIsdaFixPm ()
{
    if (_ppUsdLiborSwapIsdaFixPm != NULL)
    {
	    delete _ppUsdLiborSwapIsdaFixPm;
    }
}
QuantLib::UsdLiborSwapIsdaFixPm& Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::GetReference ()
{
    if (_ppUsdLiborSwapIsdaFixPm == NULL) throw REFNEW NativeNullException ();
	return **_ppUsdLiborSwapIsdaFixPm;
}
boost::shared_ptr<QuantLib::UsdLiborSwapIsdaFixPm>& Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::GetShared ()
{
    if (_ppUsdLiborSwapIsdaFixPm == NULL) throw REFNEW NativeNullException ();
	return *_ppUsdLiborSwapIsdaFixPm;
}
QuantLib::UsdLiborSwapIsdaFixPm* Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::GetPointer ()
{
    if (_ppUsdLiborSwapIsdaFixPm == NULL) throw REFNEW NativeNullException ();
	return &**_ppUsdLiborSwapIsdaFixPm;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::UsdLiborSwapIsdaFixPm>& Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::GetHandle ()
{
	if (_phUsdLiborSwapIsdaFixPm == NULL)
	{
		_phUsdLiborSwapIsdaFixPm = new Handle<QuantLib::UsdLiborSwapIsdaFixPm> (*_ppUsdLiborSwapIsdaFixPm);
	}
	return *_phUsdLiborSwapIsdaFixPm;
}
#endif
bool Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm::HasNative () 
{
	return (_ppUsdLiborSwapIsdaFixPm != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Indexes::Swap::IUsdLiborSwapIsdaFixPm^ Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm_Factory::Create (Cephei::QL::Times::IPeriod^ tenor, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ h)
{
    return REFNEW CUsdLiborSwapIsdaFixPm ( tenor,  h);
}
Cephei::QL::Indexes::Swap::IUsdLiborSwapIsdaFixPm^ Cephei::QL::Indexes::Swap::CUsdLiborSwapIsdaFixPm_Factory::Create (Cephei::QL::Times::IPeriod^ tenor, Cephei::QL::Termstructures::IYieldTermStructure^ forwarding, Cephei::QL::Termstructures::IYieldTermStructure^ discounting)
{
    return REFNEW CUsdLiborSwapIsdaFixPm ( tenor,  forwarding,  discounting);
}
